package com.venus.finance.transferaccount.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.DateUtil;
import com.venus.component.page.PageInfo;
import com.venus.finance.account.service.IAccountService;
import com.venus.finance.transferaccount.form.TransferAccountForm;
import com.venus.finance.transferaccount.service.ITransferAccountService;
import com.venus.model.AccountVO;
import com.venus.model.PubUserVO;
import com.venus.model.TransferAccountVO;

public class TransferAccountMultiOperateAction extends BaseAction {

	private ITransferAccountService transferAccountService ;
	
	private IAccountService accountService;

	public void setTransferAccountService(ITransferAccountService transferAccountService) { this.transferAccountService = transferAccountService; }

	public void setAccountService(IAccountService accountService) { this.accountService = accountService; }
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		String strMethod = transferAccountForm.getMethod();
		if("list".equals(strMethod)){
			return this.list(mapping, transferAccountForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, transferAccountForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, transferAccountForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, transferAccountForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, transferAccountForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, transferAccountForm, request, response);
		}
		return super.execute(mapping, form, request, response);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		PubUserVO objPubUserVO = this.getLoginUser(request);
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 只显示当前用户的数据
		lstConditionUtils.add(new ConditionUtil("and", "pubUser.userId", "=", objPubUserVO.getUserId()+"", false));
		if(transferAccountForm.getFromAccountId() != 0){ // 转出账户
			lstConditionUtils.add(new ConditionUtil("and", "fromAccount.accountId", "=", transferAccountForm.getFromAccountId()+"", false));
		}
		if(transferAccountForm.getToAccountId() != 0){ // 转入账户
			lstConditionUtils.add(new ConditionUtil("and", "toAccount.accountId", "=", transferAccountForm.getToAccountId()+"", false));
		}
		// 开始时间
		if(!"".equals(transferAccountForm.getStartDate())){
			lstConditionUtils.add(new ConditionUtil("and", "transferDate", ">=", transferAccountForm.getStartDate()+"", true));
		}
		// 结束时间
		if(!"".equals(transferAccountForm.getEndDate())){
			lstConditionUtils.add(new ConditionUtil("and", "transferDate", "<=", transferAccountForm.getEndDate()+"", true));
		}
		// 金额范围:最小值
		if(0 != transferAccountForm.getMinAmount()){
			lstConditionUtils.add(new ConditionUtil("and", "amount", ">=", transferAccountForm.getMinAmount()+"", false));
		}
		// 金额范围:最大值
		if(0 != transferAccountForm.getMaxAmount()){
			lstConditionUtils.add(new ConditionUtil("and", "amount", "<=", transferAccountForm.getMaxAmount()+"", false));
		}
		// 按日期倒序
		lstConditionUtils.add(new ConditionUtil("order by", "transferDate", "", "desc", false));
		// 得到分页信息
		PageInfo objPageInfo = this.transferAccountService.getPageInfo(transferAccountForm.getPageRecord(), transferAccountForm.getCurPage(), lstConditionUtils);
		request.setAttribute("objPageInfo", objPageInfo);
		request.setAttribute("lstTransferAccountVOs", objPageInfo.getLstRecords());
		// 页面查询条件: 转出账户
		List<AccountVO> lstFormAccounts = this.accountService.findAccountByUser(objPubUserVO);
		request.setAttribute("lstFormAccounts", lstFormAccounts);
		// 页面查询条件: 转入账户
		List<AccountVO> lstToAccounts = this.accountService.findAccountByGroup(objPubUserVO.getUserGroup());
		request.setAttribute("lstToAccounts", lstToAccounts);
		return mapping.findForward("list");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		this.saveToken(request);
		// 转出账户列表,当前用户的账户
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		lstConditionUtils.add(new ConditionUtil("order by", "sort", "", "", false));
		List<AccountVO> lstAccountVOs = this.accountService.findAccountVOs(lstConditionUtils);
		//List<AccountVO> lstAccountVOs = this.accountService.findAccountVOs(null);
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		return mapping.findForward("create");
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		TransferAccountVO objTransferAccountVO = new TransferAccountVO();
		objTransferAccountVO.setPubUser(this.getLoginUser(request));
		AccountVO fromAccountVO = this.accountService.loadAccountVO(transferAccountForm.getFromAccountId());
		AccountVO toAccountVO = this.accountService.loadAccountVO(transferAccountForm.getToAccountId());
		objTransferAccountVO.setFromAccount(fromAccountVO);
		objTransferAccountVO.setToAccount(toAccountVO);
		objTransferAccountVO.setAmount(transferAccountForm.getAmount());
		objTransferAccountVO.setHandFee(transferAccountForm.getHandFee());
		objTransferAccountVO.setTransferDate(DateUtil.parseDate(transferAccountForm.getTransferDate(), "yyyy-MM-dd"));
		objTransferAccountVO.setNote(transferAccountForm.getNote());
		// 修改转出账户和转入账户的余额
		this.transferAccountService.saveTransferAccount(objTransferAccountVO);
		// 新增转账记录实例
		//this.transferAccountService.saveOrUpdateTransferAccountVO(objTransferAccountVO);
		// 页面上新增记录展现特别标识
		request.setAttribute("newId", objTransferAccountVO.getTransferAccountId());
		// 参数清除,防止作为列表查询条件
		transferAccountForm.reset(mapping, request);
		return this.list(mapping, transferAccountForm, request, response);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		// 查询转账实例
		TransferAccountVO objTransferAccountVO = this.transferAccountService.findOneTransferAccountVO(transferAccountForm.getTransferAccountId());
		request.setAttribute("objTransferAccountVO", objTransferAccountVO);
		// 查询账户列表
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		lstConditionUtils.add(new ConditionUtil("order by", "sort", "", "", false));
		List<AccountVO> lstAccountVOs = this.accountService.findAccountVOs(lstConditionUtils);
		//List<AccountVO> lstAccountVOs = this.accountService.findAccountVOs(null);
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		return mapping.findForward("edit");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		// 修改转账记录实例
		this.transferAccountService.updateTransferAccount(transferAccountForm);
		// 页面上新增记录展现特别标识
		request.setAttribute("updateId", transferAccountForm.getTransferAccountId());
		// 参数清除,防止作为列表查询条件
		transferAccountForm.reset(mapping, request);
		return this.list(mapping, transferAccountForm, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		TransferAccountForm transferAccountForm = (TransferAccountForm)form;
		this.transferAccountService.deleteTransferAccount(transferAccountForm.getTransferAccountIds());
		return this.list(mapping, form, request, response);
	}
}
